package cn.jji8.mysqlUse.mySqlSimpleUse.queryResults;

import cn.jji8.mysqlUse.mySqlSimpleUse.column.Conlumn;
import cn.jji8.mysqlUse.mySqlSimpleUse.column.DoubleConlumn;
import cn.jji8.mysqlUse.mySqlSimpleUse.column.IntConlumn;
import cn.jji8.mysqlUse.mySqlSimpleUse.column.TextConlumn;
import cn.jji8.mysqlUse.mySqlSimpleUse.error.ErrorColumnException;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 查询的结果
 * */
public class QueryResults {
    Map<TextConlumn,Map<Integer,String>> textConlumnMap = new HashMap<>();
    Map<IntConlumn,Map<Integer,Integer>> intConlumnMap = new HashMap<>();
    Map<DoubleConlumn,Map<Integer,Double>> doubleConlumnMap = new HashMap<>();
    public QueryResults(ResultSet resultSet, Set<Conlumn> conlumnSet) {
        //分类存放
        for (Conlumn conlumn : conlumnSet) {
            if(conlumn instanceof TextConlumn){
                textConlumnMap.put((TextConlumn)conlumn,new HashMap<>());

            }else if(conlumn instanceof IntConlumn){
                intConlumnMap.put((IntConlumn)conlumn,new HashMap<>());

            }else if(conlumn instanceof  DoubleConlumn){
                doubleConlumnMap.put((DoubleConlumn)conlumn,new HashMap<>());

            }
        }
        //分类取数据
        try {
            if(resultSet!=null){
                while (resultSet.next()) {
                    int id = resultSet.getInt("identifier");
                    for (TextConlumn textConlumn : textConlumnMap.keySet()) {
                        textConlumnMap.get(textConlumn).put(id,resultSet.getString(textConlumn.getConlumnName()));
                    }
                    for (IntConlumn intConlumn : intConlumnMap.keySet()) {
                        intConlumnMap.get(intConlumn).put(id,resultSet.getInt(intConlumn.getConlumnName()));
                    }
                    for (DoubleConlumn doubleConlumn : doubleConlumnMap.keySet()) {
                        doubleConlumnMap.get(doubleConlumn).put(id,resultSet.getDouble(doubleConlumn.getConlumnName()));
                    }
                }
            }
        }catch (SQLException throwables) {
                throwables.printStackTrace();
                throw new ErrorColumnException("某一列的数据类型",throwables);
        }
        try {
            if(resultSet!=null){
                resultSet.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            System.out.println("关闭资源时发生错误。");
        }
    }


    /**
     * 获取查询到对应列的数据 Map[ID,数据]
     * @return 如果没有数据会返回空map，如果没有查此表，会返回null
     * */
    public Map<Integer,String> getResults(TextConlumn textConlumn){
        return textConlumnMap.get(textConlumn);
    }
    /**
     * 获取查询到对应列的数据 Map[ID,数据]
     * @return 如果没有数据会返回空map，如果没有查此表，会返回null
     * */
    public Map<Integer, Integer> getResults(IntConlumn intConlumn) {
        return intConlumnMap.get(intConlumn);
    }
    /**
     * 获取查询到对应列的数据 Map[ID,数据]
     * @return 如果没有数据会返回空map，如果没有查此表，会返回null
     * */
    public Map<Integer, Double> getResults(DoubleConlumn valueConlumn) {
        return doubleConlumnMap.get(valueConlumn);
    }
}
